package com.wing.bill.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.bill.dao.UsingInReportDao;
import com.wing.bill.dao.UsingReportDao;
import com.wing.cabinet.dao.UseInventoryInoutDao;
import com.wing.cabinet.model.response.StorageInventoryInoutVO;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.LocalDateTimeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UsingInReportServiceImpl implements UsingInReportService {
    @Autowired
    private UsingReportDao usingReportDao;

    @Autowired
    private UsingInReportDao usingInReportDao;

    @Autowired
//    private StorageInventoryInoutDao storageInventoryInoutDao;
    private UseInventoryInoutDao useInventoryInoutDao;

    @Override
    public JsonResult list(Integer type,String param,  String enterpriseCode) {

        HashMap hm = new HashMap();
        hm.put("title","物料在用统计(按天)");
        hm.put("data", usingInReportDao.list(type,param,enterpriseCode));
        return JsonResult.success(hm);
    }

    @Override
    public JsonResult topCell(Integer type,String startTime, String endTime, Integer click, String mainCabCode,String enterpriseCode) {
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");
            String title = "";
            if (type == null){
                title = "在用刀具";
            } else  if (type != null ){
                title = "报失物料";
            }
            if (click == 0) {
                HashMap map1 = usingInReportDao.queryNumAndAmount(type,"qua",start, end, mainCabCode,enterpriseCode);
                map1.put("title", title+"总数(支)");
                map1.put("sign", "1");

                HashMap map2 = usingInReportDao.queryNumAndAmount(type,"money",start, end, mainCabCode,enterpriseCode);
                map2.put("title", title+"总金额(元)");
                map2.put("sign", "2");


                List list = new ArrayList();
                list.add(map1);
                list.add(map2);
                return JsonResult.success("查询成功", list);
            } else if (click == 1) {
                return JsonResult.success("查询成功",usingInReportDao.queryNumAndAmountClick(type,start, end, mainCabCode,enterpriseCode));
            } else if (click == 2) {
                return JsonResult.success("查询成功",usingInReportDao.queryNumAndAmountClick(type,start, end, mainCabCode,enterpriseCode));
            }
        }
        return null;
    }

    @Override
    public JsonResult queryStatisticsByParam(Integer type, String selectParam, String param, String startTime, String endTime, String mainCabCode, String enterpriseCode, String viewType, String tab, Map<String, Object> screenParam) {
        String title = "";
        if (type == null){
            title = "在用刀具";
        } else  if (type != null ){
            title = "物料报失";
        }

        HashMap hm = new HashMap();
        if ("tab0".equals(tab)) {
            if ("sku".equals(viewType)) {
                hm.put("title", title+"排名(按物料)");
            } else if ("cab".equals(viewType)) {
                hm.put("title", title+"排名(按柜子)");
            } else if ("org".equals(viewType)) {
                hm.put("title", title+"排名(按组织)");
            } else if ("use".equals(viewType)) {
                hm.put("title", title+"排名(按使用情况)");
            } 
        } else if ("tab1".equals(tab) && "spu".equals(selectParam)) {
            hm.put("title", title+"排名(按系列SPU)");
        } else if ("tab1".equals(tab) && "sku".equals(selectParam)) {
            hm.put("title", title+"排名(按SKU)");
        } else if ("tab1".equals(tab) && "type".equals(selectParam)) {
            hm.put("title", title+"排名(按类型)");
        } else if ("tab1".equals(tab) && "category".equals(selectParam)) {
            hm.put("title", title+"排名(按分类)");
        } else if ("tab1".equals(tab) && "brand".equals(selectParam)) {
            hm.put("title", title+"排名(按品牌)");
        } else if ("tab1".equals(tab) && "main".equals(selectParam)) {
            hm.put("title", title+"排名(按主柜)");
        } else if ("tab1".equals(tab) && "desk".equals(selectParam)) {
            hm.put("title", title+"排名(按副柜)");
        }  else if ("tab1".equals(tab) && "person".equals(selectParam)) {
            hm.put("title", title+"排名(按人)");
        } else if ("tab1".equals(tab) && "department".equals(selectParam)) {
            hm.put("title", title+"排名(按部门)");
        } else if ("tab1".equals(tab) && "costCenter".equals(selectParam)) {
            hm.put("title", title+"排名(按成本中心)");
        } else if ("tab1".equals(tab) && "workOrder".equals(selectParam)) {
            hm.put("title", title+"排名(按工单)");
        } else if ("tab1".equals(tab) && "workShop".equals(selectParam)) {
            hm.put("title", title+"排名(按车间)");
        } else if ("tab1".equals(tab) && "device".equals(selectParam)) {
            hm.put("title", title+"排名(按设备)");
        }


        List<HashMap> list = new ArrayList<>();
        if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)) {
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");
            
            list.addAll(usingInReportDao.queryStatisticsByParam(type, selectParam, param, start, end, mainCabCode, enterpriseCode));
            if ("department".equals(selectParam)||"costCenter".equals(selectParam)){
                hm.put("totalSum",usingInReportDao.getTotal(type,start,end,param,mainCabCode,enterpriseCode));
            }

        }

        hm.put("data",list);
        return JsonResult.success("查询成功",hm);
    }

    @Override
    public Page data(Integer type, String startTime, String endTime, String mainCabCode, String enterpriseCode, Page page) {
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {

            Map<String, Object> params = new HashMap<>();
            params.put("type",type);
            params.put("startCreateTime",startTime);
            params.put("endCreateTime",endTime);
            params.put("mainCabCode",mainCabCode);
            params.put("enterpriseCode",enterpriseCode);

            List<StorageInventoryInoutVO> list = useInventoryInoutDao.list4app(page,params);

            page.setRecords(list);
            return page;
        }
        return null;
    }
}
